package seta.minecredit;

import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import com.iConomy.iConomy;

public class MineCredit extends JavaPlugin implements Listener {
	Logger log = Logger.getLogger("Minecraft");
	private seta.minecredit.Files Filer;
	public static boolean doloop = true;
	private CreditLoop cl;
	private xPlayerListener pl;
	private ReminderLoop rl;
	
	@SuppressWarnings("deprecation")
	@Override
	public void onDisable() {
		cl.interrupt();
		cl.stop();
		rl.interrupt();
		rl.stop();
		log.info("MineCredit disabled.");
	}

	@Override
	public void onEnable() {		
		Filer = new Files();
		Filer.createInit();
		Filer.loadContent();
		
		cl = new CreditLoop(Filer);
		cl.start();
		
    	pl = new xPlayerListener(Filer);
		
		PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_CHAT, pl, Priority.High, this);
        
		if(Filer.getConfKey("remind").equals("true")){
			rl = new ReminderLoop(Filer);
			rl.start();
		}
        
		log.info("MineCredit enabled.");
	}

	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
		if ((cmd.getName().equalsIgnoreCase("credit") || (Filer.getConfKey("enableshortcut").equals("true") && cmd.getName().equalsIgnoreCase("c"))) && ((sender.hasPermission("minecredit.user") || sender.isOp() || Filer.getConfKey("usepermission").equals("false")))) {
			String user = sender.toString().split("=")[1].replace("}","");
			if(args.length > 0){
				if(args[0].equals("pay") || args[0].equals("p")){
					if(args.length > 1){
						if(args[1].equals("all")){
							if(iConomy.getAccount(user).getHoldings().hasEnough(Integer.parseInt(Filer.getCreditKey(user)))){
								String cred = Filer.getCreditKey(user);
								double bal = iConomy.getAccount(user).getHoldings().balance()-Integer.parseInt(Filer.getCreditKey(user));
								iConomy.getAccount(user).getHoldings().set(bal);
								Filer.setUserCredit(user, "0");
								sender.sendMessage(Filer.getLangKey("paybackall",cred,bal+""));
							} else {
								sender.sendMessage(Filer.getLangKey("nomoney"));
							}
						} else {
							if(iConomy.getAccount(user).getHoldings().hasEnough(Integer.parseInt(args[1]))){
								if(Integer.parseInt(args[1]) >= Integer.parseInt(Filer.getCreditKey(user))){
									args[1] = Filer.getCreditKey(user);
								}
								double bal = iConomy.getAccount(user).getHoldings().balance()-Integer.parseInt(args[1]);
								iConomy.getAccount(user).getHoldings().set(bal);
								int cred = Integer.parseInt(Filer.getCreditKey(user))-Integer.parseInt(args[1]);
								Filer.setUserCredit(user, cred+"");
								sender.sendMessage(Filer.getLangKey("payback",cred+"",bal+""));
							} else {
								sender.sendMessage(Filer.getLangKey("nomoney"));
							}
						}
					} else {
						sender.sendMessage(Filer.getLangKey("usage")+" /credit pay|p "+Filer.getLangKey("amount"));
					}
				} else if(args[0].equals("get") || args[0].equals("g")){
					if(args.length > 1){
						if(!Filer.hasCredit(user)){
							Filer.setUserCredit(user,"0");
						}
						if((Integer.parseInt(Filer.getCreditKey(user))+Integer.parseInt(args[1])) < Integer.parseInt(Filer.getConfKey("maxcredit"))){
							sender.sendMessage(Filer.getLangKey("granted",args[1]));
							iConomy.getAccount(user).getHoldings().add(Integer.parseInt(args[1]));
							Filer.addUserCredit(user, args[1]);
							sender.sendMessage(Filer.getLangKey("grantedinfo",Filer.getCredit().getProperty(user),iConomy.getAccount(user).getHoldings().balance()+""));
						} else {
							sender.sendMessage(Filer.getLangKey("notgranted",args[1]));
							sender.sendMessage(Filer.getLangKey("notgrantedinfo"));
							sender.sendMessage(Filer.getLangKey("notgrantedinfo2",Filer.getConfKey("maxcredit"),Filer.getCredit().getProperty(user)));
						}
					} else {
						sender.sendMessage(Filer.getLangKey("usage")+" /credit get|g "+Filer.getLangKey("amount"));
					}
				} else if(args[0].equals("info") || args[0].equals("i")){
					if(Filer.hasCredit(user) && Integer.parseInt(Filer.getCreditKey(user)) > 0){
						sender.sendMessage(ChatColor.DARK_AQUA + "~~~~~~~~~~~~~~[ " + Filer.getLangKey("creditinfo") + ChatColor.DARK_AQUA + " ]~~~~~~~~~~~~~~");
						sender.sendMessage(Filer.getLangKey("yourecredit",Filer.getCredit().getProperty(user),Filer.getConfKey("maxcredit")));
						sender.sendMessage(Filer.getLangKey("yourecredit2",Filer.getConfKey("badcredit")));
						sender.sendMessage(Filer.getLangKey("yourecredit3",Filer.getConfKey("interest")));
					} else {
						sender.sendMessage(Filer.getLangKey("nocredit"));
					}
				} else if(args[0].equals("rate") || args[0].equals("r")){
					sender.sendMessage(Filer.getLangKey("yourecredit3",Filer.getConfKey("interest")));
				} else if(args[0].equals("max") || args[0].equals("m")){
					sender.sendMessage(Filer.getLangKey("yourecreditmax",Filer.getConfKey("maxcredit")));
				} else if(args[0].equals("bad") || args[0].equals("b")){
					sender.sendMessage(Filer.getLangKey("yourecredit2",Filer.getConfKey("badcredit")));
				}
			} else {
				sender.sendMessage(ChatColor.DARK_AQUA + "~~~~~~~~~~~~~~[ " + Filer.getLangKey("creditinfo") + ChatColor.DARK_AQUA + " ]~~~~~~~~~~~~~~");
				sender.sendMessage(ChatColor.GREEN+" /credit get|g "+ChatColor.LIGHT_PURPLE+"AMOUNT"+ChatColor.GOLD+" - "+Filer.getLangKey("cmd_get"));
				sender.sendMessage(ChatColor.GREEN+" /credit pay|p "+ChatColor.LIGHT_PURPLE+"AMOUNT"+ChatColor.GOLD+" - "+Filer.getLangKey("cmd_pay"));
				sender.sendMessage(ChatColor.GREEN+" /credit info|i  "+ChatColor.GOLD+"- "+Filer.getLangKey("cmd_info"));
				sender.sendMessage(ChatColor.GREEN+" /credit rate|r "+ChatColor.GOLD+"- "+Filer.getLangKey("cmd_rate"));
				sender.sendMessage(ChatColor.GREEN+" /credit max|m  "+ChatColor.GOLD+"- "+Filer.getLangKey("cmd_max"));
				sender.sendMessage(ChatColor.GREEN+" /credit bad|b  "+ChatColor.GOLD+"- "+Filer.getLangKey("cmd_bad"));
				if(Filer.getConfKey("enableshortcut").equals("true")){
					sender.sendMessage(" "+Filer.getLangKey("alsocan","/c","/credit"));
				}
			}
		} else if ((cmd.getName().equalsIgnoreCase("credita") || (Filer.getConfKey("enableadminshortcut").equals("true") && cmd.getName().equalsIgnoreCase("ca"))) && (sender.hasPermission("minecredit.admin") || sender.isOp())) {
			if(args.length > 0){
				if(args[0].equals("reload") || args[0].equals("r")){
					Filer.reload();
					sender.sendMessage(Filer.getLangKey("reload"));
				} else if(args[0].equals("options") || args[0].equals("o")){
					sender.sendMessage(ChatColor.DARK_AQUA + "~~~~~~~~~~~~~~[ " + Filer.getLangKey("creditoptions") + ChatColor.DARK_AQUA + " ]~~~~~~~~~~~~~~");
					sender.sendMessage(ChatColor.GOLD+" language "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("language")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_language"));
					sender.sendMessage(ChatColor.GOLD+" interest "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("interest")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_interest"));
					sender.sendMessage(ChatColor.GOLD+" maxcredit "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("maxcredit")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_maxcredit"));
					sender.sendMessage(ChatColor.GOLD+" badcredit "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("badcredit")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_badcredit"));
					sender.sendMessage(ChatColor.GOLD+" interesttime "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("interesttime")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_interesttime"));
					sender.sendMessage(ChatColor.GOLD+" bad_grayout "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("bad_grayout")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_grayout"));
					sender.sendMessage(ChatColor.GOLD+" bad_onfire "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("bad_onfire")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_onfire"));
					sender.sendMessage(ChatColor.GOLD+" enableshortcut "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("enableshortcut")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_enableshortcut"));
					sender.sendMessage(ChatColor.GOLD+" enableadminshortcut "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("enableadminshortcut")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_enableadminshortcut"));
					sender.sendMessage(ChatColor.GOLD+" usepermission "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("usepermission")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_usepermission"));
					sender.sendMessage(ChatColor.GOLD+" remind "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("remind")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_remind"));
					sender.sendMessage(ChatColor.GOLD+" remindtime "+ChatColor.DARK_GREEN+"= "+ChatColor.GRAY+Filer.getConfKey("remindtime")+""+ChatColor.GOLD+" - "+Filer.getLangKey("opt_remindtime"));
				} else if(args[0].equals("set")){
					if(args.length > 2){
						if(Filer.hasConfigKey(args[1])){
							Filer.setConfig(args[1], args[2]);
							Filer.reload();
							sender.sendMessage(Filer.getLangKey("optionset",args[1]+"",args[2]+""));
						} else {
							sender.sendMessage(Filer.getLangKey("unknownoption"));
						}
					} else {
						sender.sendMessage(Filer.getLangKey("usage")+" /credita set "+Filer.getLangKey("optval"));
					}
				}
			} else {
				sender.sendMessage(ChatColor.DARK_AQUA + "~~~~~~~~~~~~~~[ " + Filer.getLangKey("creditinfo") + ChatColor.DARK_AQUA + " ]~~~~~~~~~~~~~~");
				sender.sendMessage(ChatColor.GREEN+" /credita set "+ChatColor.LIGHT_PURPLE+"OPTION VALUE "+ChatColor.GOLD+"- "+Filer.getLangKey("acmd_set"));
				sender.sendMessage(ChatColor.GREEN+" /credita options|o "+ChatColor.GOLD+"- "+Filer.getLangKey("acmd_options"));
				sender.sendMessage(ChatColor.GREEN+" /credita reload|r "+ChatColor.GOLD+"- "+Filer.getLangKey("acmd_reload"));
				if(Filer.getConfKey("enableadminshortcut").equals("true")){
					sender.sendMessage(" "+Filer.getLangKey("alsocan","/ca","/credita"));
				}
			}
		}
		return true; 
	}

}